package com.lagou.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.concurrent.Callable;

public class UserClientHandler extends ChannelInboundHandlerAdapter implements Callable {

    private ChannelHandlerContext context;
    private String result;
    private Object para;

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        context = ctx;
    }

    /**
     * 收到服务端数据，唤醒等待线程
     */
    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) {
        System.out.println("------0000000000000---------");
        result = msg.toString();
        System.out.println("------1111111111111111111111111---------");

        notify();
    }

    /**
     * 写出数据，开始等待唤醒
     */

    public synchronized Object call() throws InterruptedException {
        System.out.println("------000000000----");
        context.writeAndFlush(para);
        System.out.println("------000000000----");
        wait();
        System.out.println("------11111----");
        return result;
    }

    /*
     设置参数
     */
    void setPara(String para) {
        this.para = para;
    }

    void setPara(Object para) {

        this.para = para;

    }


}
